

import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import s_get from '@dfeidao/atom-web/storage/get';

import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import get_data from '../../atom/get-data';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import P01 from './P01';
import P03 from './P03';
import P04 from './P04';
import P05 from './P05';


function chunk(arr: object[], size: number) {
	const arr2 = new Array();
	for (let i = 0; i < arr.length; i = i + size) {
		const arr1 = arr;
		arr2.push(arr1.slice(i, i + size));
	}
	return arr2;
}
interface Aa {
	count: number;
	page_no: number;
	page_size: number;
	v: string;
	arr: string[];
}
export default async function a000(fd: IFeidaoAiBrowserComponent, comp_id: string, event_no: string, role_type_id: string, fei: string) {
	//  调用事件 'zj-000008', 'ec-002', role_type_id, f.field.value
	// role_type_id: string, field_value: string
	fd.data.node.querySelector<HTMLDivElement>('[id="zj9"]').classList.remove('display-n');
	// debugger;

	set(fd, 'condition_node', { param: {} });
	set(fd, 'condition_evt', { param: {} });
	set(fd, 'criteria', {});
	set(fd, 'page_no', 1);
	set(fd, 'page_size', 1);

	set(fd, 'comp_id', comp_id);
	set(fd, 'event_no', event_no);
	set(fd, 'role_type_id', role_type_id);
	set(fd, 'fei', fei);
	const productid = s_get('productid');
	set(fd, 'productid', productid);
	show_loading();
	const res = await nodejs<{ length: number }>('roler/zj-000009/s001', { productid, role_type_id }) as Array<{}>;
	hide_loading();
	if (res.length === 0) {
		render(fd, '', P01, 'P01', 'inner');
	} else {
		set(fd, 'biaotou', res);
		render(fd, res, P01, 'P01', 'inner');
	}
	const obj = {};
	const sort = {};
	// sort[args[3]] = 1;
	set(fd, 'sort', sort);
	const arr = res.map((a: { field: string, fieldtitle: string }, i) => {
		obj[a.field] = a.fieldtitle;
		return a.field;
	});
	set(fd, 'name2title', obj);
	show_loading();
	const res1 = await nodejs<{ length: number }>("roler/zj-000009/s002", { productid, role_type_id }) as Array<{ systemid: string, modelid: string, tablename: string }>;
	hide_loading();
	if (res1.length === 0) {
	} else {
		// console.log(sort)
		const aa = await get_data(res1[0].systemid, null, res1[0].modelid, res1[0].tablename, {
			paging: {
				index: 1,
				size: 0
			},
			param: { productid },
			sort
		});
		if (aa.length === 0) {
			render(fd, '', P03, 'P03', 'inner');
			render(fd, '', P04, 'P04', 'inner');
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P05"]').innerHTML = '';
		} else {
			const data = aa.map((a: Aa) => {
				const objj = {} as Aa;
				arr.map((v) => {
					objj[v] = a[v];
				});
				objj.v = JSON.stringify(objj);
				objj.arr = arr;
				return objj;
			});
			const dataArray = chunk(data, 10);
			const fenye = {
				count: data.length,
				page_no: 1,
				page_size: 10
			};

			render(fd, fenye, P05, 'P05', 'inner');
			render(fd, dataArray[0], P03, 'P03', 'inner');
		}
	}
}
